﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using exsportance.Silverlight.Common.Helper;

namespace exsportance.Silverlight.Common.Utils
{
    public static class AppUtils
    {
        public static string DebugRootUrl = "";
        public static string AlterRootUrl = "";

        /// <summary>
        /// Returns the root path
        /// </summary>
        /// <returns></returns>
        public static string GetApplicationRoot()
        {
            string result = "";
            //HtmlPage.Document.DocumentUri

            if (System.Diagnostics.Debugger.IsAttached && !DebugRootUrl.IsNullOrEmpty())
            {
                result = DebugRootUrl;
            }
            else
            {
                if (string.IsNullOrEmpty(AlterRootUrl))
                {
                    if (UISynchronizationContext.Instance != null && UISynchronizationContext.Instance.IsValid)
                    {
                        UISynchronizationContext.Instance.InvokeSynchronously(() =>
                            {
                                result = Application.Current.Host.Source.ToString();
                            });
                    }
                    else
                        result = Application.Current.Host.Source.ToString();
                }
                else
                    result = AlterRootUrl;

                if (result.Length > 0 && result.LastIndexOf("/") > -1)
                    result = result.Substring(0, result.LastIndexOf("/"));
            }

            return result;
        }


    }
}
